package com.software.graduationproject.modules.food.service;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.Page;

import com.google.common.base.Strings;
import com.software.graduationproject.commons.select.Select;
import com.software.graduationproject.commons.util.DateUtils;
import com.software.graduationproject.modules.comment.mapper.CommentMapper;
import com.software.graduationproject.modules.comment.object.CommentVo;
import com.software.graduationproject.modules.food.entity.Food;
import com.software.graduationproject.modules.food.mapper.FoodMapper;
import com.software.graduationproject.modules.food.object.FoodCommentVo;
import com.software.graduationproject.modules.food.object.FoodVo;
import com.software.graduationproject.modules.food.object.FoodCreateQo;
import com.software.graduationproject.modules.food.object.FoodUpdateQo;

import com.software.graduationproject.modules.marketers.service.MarketersService;
import com.software.graduationproject.modules.user.entity.User;
import com.software.graduationproject.modules.user.mapper.UserMapper;
import com.software.graduationproject.modules.user.object.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * FoodService : 食品表
 * ---------------------------------------------------------------------------------------------------------------------
 */
@Service
public class FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private MarketersService marketersService;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    public void createQo(FoodCreateQo foodCreateQo) {
        String foodImg = foodCreateQo.getFoodImg();
        // 解码Base64字符串为字节数组
        byte[] decodedBytes = Base64.getDecoder().decode(foodImg);
        // 创建 MockMultipartFile 对象
        MultipartFile file = new MockMultipartFile("img.jpg", decodedBytes);
        String upload = marketersService.upload(file);
        Food food = new Food();
        BeanUtil.copyProperties(foodCreateQo, food);
        food.setFoodImg(upload);
        foodMapper.insert(food);
    }

    public FoodVo selectVo(Long id) {
        FoodVo foodVo = foodMapper.selectDetail(id);
        return foodVo;
    }

    public FoodCommentVo selectFoodComment(Long id) {
        FoodCommentVo foodCommentVo = foodMapper.selectFoodComment(id);
        String commentsId = foodCommentVo.getCommentsId();
        if (Strings.isNullOrEmpty(commentsId)) {
            return foodCommentVo;
        }
        String[] split1 = commentsId.split(",");
        List<Long> ids = Arrays.stream(split1)
                .map(Long::parseLong)
                .collect(Collectors.toList());
        List<CommentVo> commentsByIds = commentMapper.findCommentsByIds(ids);
        // 根据评论的userId查询对应的用户信息，并存储在Map中
       List<UserVo> list = new ArrayList<>();
        for (CommentVo comment : commentsByIds) {
            Long userId = comment.getUserId();
            UserVo userVo = new UserVo();
            User user = userMapper.selectById(userId);
            userVo.setId(user.getId());
            userVo.setNickname(user.getNickname());
            userVo.setPicUrl(user.getPicUrl());
            userVo.setTime(DateUtils.formatDateToDate(comment.getCreatedDate()));
            // 将评论添加到用户的评论列表中
            userVo.setCommentVo(comment);
            list.add(userVo);
        }
        foodCommentVo.setUserVos(list);
        return foodCommentVo;
    }

    public Page<FoodVo> selectPageVo(Select select) {
        return foodMapper.selectPageVo(select);
    }

    public List<FoodVo> selectListVo(Select select) {
        return foodMapper.selectListVo(select);
    }

    public List<FoodVo> searchAllBySelect(Select select) {
        return foodMapper.searchAllBySelect(select);
    }

}
